package com.zdpx.pxframework.seata.rm.datasource.undo.dm;

import io.seata.common.loader.LoadLevel;
import io.seata.rm.datasource.undo.oracle.OracleUndoLogManager;
import io.seata.sqlparser.util.JdbcConstants;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * undo_log表数据插入日志脚本与oracle有所不同，进行输配
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2021-04-22 9:13:18
 */
@LoadLevel(name = JdbcConstants.ORACLE)
public class DmUndoLogManager extends OracleUndoLogManager {
    private static final String INSERT_UNDO_LOG_SQL_4_DM = "INSERT INTO " + UNDO_LOG_TABLE_NAME + "\n" +
            "\t(branch_id, xid, \"CONTEXT\", rollback_info, log_status, log_created, log_modified)\n" +
            "VALUES (?, ?,?, ?, ?, sysdate, sysdate)";

    public DmUndoLogManager() throws IllegalAccessException, NoSuchFieldException {
        Field field = null;
        Field modifiersField = null;
        try {
            field = this.getClass().getSuperclass().getDeclaredField("INSERT_UNDO_LOG_SQL");
            field.setAccessible(true);

            modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

            field.set(this, INSERT_UNDO_LOG_SQL_4_DM);
        } finally {
            if (modifiersField != null) {
                modifiersField.setInt(field, field.getModifiers() & Modifier.FINAL);
                modifiersField.setAccessible(false);
            }

            if (field != null) {
                field.setAccessible(false);
            }
        }
    }


}